//改变了用于判断是否已审核
var auditStateTemp = "";

var tableData = {
	url : ctp + "/pieceQueryController/pieceQuerySearch.shtml?recId=" + recId,
	hrefs : [[ "function=judgeReject", "workerId=WORKER_ID", "驳回", "[REJECT_STATE][已驳回=已驳回]" ]],
	propertys : [ "NAME","JOBNO","GROUPNAME","PROJECT_NAME","BEGIN_DATE","END_DATE","PIECE_NUM"],
	checkBox:["REC_ID",false],
	opers : true,
	trBgColor : ["tr-bgcolor", "REJECT_STATE=已驳回"],
	startLoad : function() {
		getListData(this);
	}
};
function searchData(curr) {
	setTagAndValue("form1", "curr", curr);
	setTagAndValue("form1","SUNNADA_PAGE_SIZE",5); 
	tableData.checkBoxValue = window.parent.$("#workerId").val();
	tableData.startLoad();
}

$(document).ready(function() {
	//getPayrollApply();
	searchData();
	//getProcessCondition();
});

//获取申请单基本信息
function getPayrollApply(){
	$.ajax({ 
	 	url: ctp + "/payrollController/getPayrollApplyByRecid.shtml",
	 	data: {"recId": recId},
		type : 'POST',
		dataType: "json", 
		success : function(result) {
			if(result != null){
				var list = result.list;
				var data = list[0];
				$("#applyTime").val(data.APPLY_TIME_CHAR);
				$("#beginDate").val(data.BEGIN_DATE);
				$("#endDate").val(data.END_DATE);
				$("#applyPerson").val(data.USER_NAME);
				if(data.AUDIT_STATE == '1' || data.AUDIT_STATE == '2'){
					auditStateTemp = data.AUDIT_STATE
					$("#auditButton").hide();
					$("#auditDesc").val(data.AUDIT_DESC);
				}
			}
		}
	});
}

//获取当前流程审核情况
function getProcessCondition(){
	$.ajax({ 
	 	url: ctp + "/processController/getProcessConditionByrecid.shtml",
	 	data: {"recId": recId,"isView":"1","processType":"payrollApply"},
		type : 'POST',
		dataType: "json", 
		success : function(result) {
			if(result != null){
				var list = result.list;
				for(var i = 0; i < list.length; i++){
					var data = list[i];
					if(data.PARENT_ID == "00"){
						addProcessContion(data);
					}
				}
				var parentId = '0';
				iterateProcess(list, parentId);
			}
		}
	});
}

//循环遍历审批流程
function iterateProcess(list, parentId){
	for(var i = 0; i < list.length; i++){
		if(list[i].PARENT_ID == parentId){
			addProcessContion(list[i]);
			iterateProcess(list, list[i].AUDIT_ID);
		}
	}
}

function addProcessContion(json){
	var auditDesc = json.AUDIT_DESC;
	var recTime = json.REC_TIME;
	if(auditDesc == undefined){
		auditDesc = "";
	}
	if(recTime == undefined){
		recTime = "";
	}
	var className = '';
	if(json.AUDIT_STATE == "审核不通过"){
		className = 'state_reject';
	}else if(json.AUDIT_STATE == "审核通过"){
		className = 'state_success';
	}
	var html = '<div class="smart-state_item">'
			+'        <div class="smart-state_cir ' + className +'">'
			+'    <i class="layui-icon">&#xe617;</i>'
			+'</div>'
			+'<div class="smart-state_content">'
			+'    <span class="smart-state_name">' + json.USER_NAME + '</span>'
			+'    <span class="sscsp ' + className +'">【' + json.AUDIT_STATE + '】</span>'
			+'    <span class="smart-state_reason">' + auditDesc + '</span>'
			+'    <p class="smart-state_time">' + recTime + '</p>';
			
	if(json.WORHER_NAME != '' && json.WORHER_NAME != null){
		html += '<p class="state_reject back">' + json.WORHER_NAME + '被退回</p>';
	}
	html += '</div></div>';
	$("#processContion").append(html);
	
}

function audit(value){
	var auditDesc = $("#auditDesc").val();
	if(auditDesc == ""){
		jalert("审核意见不能为空，请填写！");
		return;
	}
	$.ajax({ 
	 	url: ctp + "/payrollController/audit.shtml",
	 	data: {"auditId": auditId,"auditState": value,"submitId": recId,"auditDesc": auditDesc},
		type : 'POST',
		success : function(result) {
			if(result == ''){
				jalert9("审核成功！");
			}else{
				jalert(result);
			}
		}
	});
}

function judgeReject(workerId){
	if(auditStateTemp == '1' || auditStateTemp == '2'){
		jalert("该申请单已审核，不能进行驳回操作！");
		return;
	}
	jconfirm("reject('" + workerId + "')", "确定要驳回该记录？");
}

function reject(workerId){
	$.ajax({ 
	 	url: ctp + "/processController/reject.shtml",
	 	data: {"applyId": recId,"workerId": workerId,"rejectState": "2","processType":"payRollApply"},
		type : 'POST',
		success : function(result) {
			if(result == ''){
				jalert9("驳回成功！");
			}else{
				jalert(result);
			}
		}
	});
}




